home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / WASTE 1.2a4 / WASTE Demo / DialogUtils.c next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  3.8 KB  |  156 lines  |  [TEXT/CWIE]

  1. /*
  2.     WASTE Demo Project:
  3.     Dialog Utilities
  4.     
  5.     Copyright © 1993-1995 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __DIALOGS__
  12. #include <Dialogs.h>
  13. #endif
  14.  
  15. #ifndef __WEDEMOAPP__
  16. #include "WEDemoIntf.h"
  17. #endif
  18.  
  19. // static variable for GetMyStandardDialogFilter
  20.  
  21. static ModalFilterUPP    sMyStandardDialogFilterProc;
  22.  
  23.  
  24. pascal Boolean    MyStandardDialogFilter( DialogRef dialog, EventRecord *event, short *item )
  25. {
  26.     GrafPtr                savePort;
  27.     ModalFilterUPP        stdFilter = NULL;
  28.     Boolean                reply = false;
  29.     OSErr                err;
  30.     Boolean                handled = false;
  31.     
  32.     // set up the port
  33.     
  34.     GetPort( &savePort );
  35.     SetGrafPortOfDialog( dialog );
  36.     
  37.     //     intercept window events directed to widnows behind the dialog
  38.     
  39.     if ( ( event->what == updateEvt ) || ( event->what == activateEvt ) )
  40.     {
  41.         if ( (DialogRef)(event->message) != dialog ) 
  42.         {
  43.             DoWindowEvent( event );
  44.         } // end if (DialogRef)whatEvent != dialog
  45.     
  46.     }
  47.     
  48.     // is the default item a pushbutton?
  49.     
  50.     if ( GetDialogItemType( dialog, GetDialogDefaultItem( dialog ) ) == kButtonDialogItem )
  51.     {
  52.         // yes, so tell the Dialog Manager to care about its outline
  53.     
  54.         err = SetDialogDefaultItem( dialog, GetDialogDefaultItem( dialog ));
  55.         if ( err != noErr )
  56.             ; // put error handling in here
  57.     }
  58.     
  59.     // this is something not in the original WASTE Demo App, but in the work that I've done
  60.     // on my own projects, I've found it useful and helpful.
  61.     
  62.     // let's also make sure the cancel button can be handled...now, the cancel button
  63.     // should be dialog item #2.  So, we get dialog item #2, check if it's a button.
  64.     // if it fills these 2 criteria, it's cancel.  Even if the default item and the
  65.     // cancel item are the same, still let them both be set this way so whatever keyboard
  66.     // keys sthe user presses will be handled properly
  67.     
  68.     // pass the number "2" to GetDialogItemType...don't check for the cancel item (cause tho
  69.     // cancel is defined as 2, we're not looking for cancel, we're looking for dialog item #2
  70.     // this is just more readable code.
  71.     
  72.     // remember, this assumes that your cancel item will be item #2 (or at least the item
  73.     // that you want to use for cancelling is #2), and that there are at least 2 items in
  74.     // the dialog to begin with!
  75.     
  76.     if ( GetDialogItemType( dialog, kStdCancelItemIndex ) == kButtonDialogItem )
  77.     {
  78.         err = SetDialogCancelItem( dialog, kStdCancelItemIndex );
  79.         if ( err != noErr )
  80.             ;  // put error handling in here
  81.     }
  82.     
  83.     
  84.     //    call the standard Dialog Manager filter procedure
  85.     
  86.  
  87.     err = GetStdFilterProc( &stdFilter );
  88.     if ( (err == noErr) && (!handled) )
  89.         reply = CallModalFilterProc( stdFilter, dialog, event, item );
  90.     
  91.     //    restore the port
  92.     SetPort( savePort );
  93.     
  94.     return reply;
  95. }
  96.  
  97.  
  98. pascal ModalFilterUPP GetMyStandardDialogFilter( void )
  99. {
  100.     if ( sMyStandardDialogFilterProc == NULL )
  101.         sMyStandardDialogFilterProc = NewModalFilterProc(MyStandardDialogFilter);
  102.     
  103.     return sMyStandardDialogFilterProc;
  104. }
  105.  
  106.  
  107.  
  108. short    GetDialogItemType( DialogRef dialog, short item )
  109. {
  110.     short        itemType;
  111.     Handle        itemHandle;
  112.     Rect        itemRect;
  113.  
  114.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  115.     
  116.     return itemType;
  117. }
  118.  
  119.  
  120. Handle    GetDialogItemHandle( DialogRef dialog, short item )
  121. {
  122.     short        itemType;
  123.     Handle        itemHandle;
  124.     Rect        itemRect;
  125.     
  126.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  127.     
  128.     return itemHandle;
  129. }
  130.  
  131. void    GetDialogItemRect( DialogRef dialog, short item, Rect *itemRect )
  132. {
  133.     short        itemType;
  134.     Handle        itemHandle;
  135.     
  136.     GetDialogItem( dialog, item, &itemType, &itemHandle, itemRect );
  137.     
  138.     return;
  139. }
  140.  
  141.  
  142. pascal void    SetDialogItemProc( DialogRef dialog, short item, UserItemUPP proc )
  143. {
  144.     short        itemType;
  145.     Handle        itemHandle;
  146.     Rect        itemRect;
  147.     
  148.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  149.     
  150.     if ( BAND( itemType, 0x007F) == userItem )
  151.         SetDialogItem( dialog, item, itemType, (Handle)proc, &itemRect );
  152.  
  153.     return;
  154. }
  155.  
  156.